#include "event.h"

namespace TinyRPC {

Event::Event(int fd, AbstractChannel *ownerChannel)
: m_fd(fd)
, m_events(0)
, m_revents(0)
, m_readTask(Task::EmptyTask())
, m_writeTask(Task::EmptyTask())
, m_status(EventStatus::INTIED)
, m_ownerChannel(ownerChannel)
{}

void Event::setWriteTask(Task&& writeTask){
    m_readTask = std::move(writeTask);
}
void Event::setReadTask(Task&& readTask){
    m_readTask = std::move(readTask);
}

void Event::setWriteTask(const Task& writeTask){
    m_readTask = writeTask;
}
void Event::setReadTask(const Task& readTask){
    m_readTask = readTask;
}
void Event::enableReading(){
    m_events |= ReadEvent;
}
void Event::enableWriting(){
    m_events |= WriteEvent;
}


void Event::disableReading(){
    m_readTask = Task::EmptyTask();
    m_events &= ~ReadEvent;
}
void Event::disableWriting(){
    m_writeTask = Task::EmptyTask();
    m_events &= ~WriteEvent;
}

void Event::enableNone(){
    m_events = NoneEvent;
    m_readTask = Task::EmptyTask();
    m_writeTask = Task::EmptyTask();
}

void Event::setRevents(int revent){
    m_revents = revent;
}
void Event::handleEvent(){
    if((m_revents & ReadEvent)){
        m_readTask.run();
    }
    if((m_revents & WriteEvent)){
        m_writeTask.run();
    }
}

void Event::reset(const Event& event){
    m_events = event.m_events;
    m_revents = event.m_revents;
    m_readTask = event.m_readTask;
    m_writeTask = event.m_writeTask;
}


}
